﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Components;
using UI.Lib.Factory;

namespace UI.ParentForm
{
    public partial class BaseTEdit : BaseEdit
    {
        public BaseTEdit()
        {
            InitializeComponent();
            _pageInit();
            init();
        }

        public BaseTEdit(string modulecode, string action)
        {
            if (!string.IsNullOrEmpty(action))
                this.Action = action;

            if (!string.IsNullOrEmpty(modulecode))
                this.ModuleCode = modulecode;

            InitializeComponent();

            _pageInit();

            init();
        }

        private void init()
        {

        }

        private void _pageInit()
        {
            // this.BackColor = Color.FromArgb(236, 241, 245);
        }

        /// <summary>
        /// 根据概要的高度，重置明细的高度
        /// </summary>
        protected void ResetSize()
        {
            int summaryHeight = 60;
            this.tbSummary.Size = new Size(this.tbSummary.Width, summaryHeight);
            int winHeight = this.Height;

            this.panDtl.Location = new Point(this.panDtl.Location.X, tspTop.Height + summaryHeight + 50);
            this.panDtl.Size = new Size(this.tbSummary.Width, winHeight - summaryHeight - tspTop.Height - 90);

            this.treeMain.Size = this.panDtl.Size;
        }

        //提取明细按钮，自动触发概要验证
        private void tspBtnTake_Click(object sender, EventArgs e)
        {
            if (IsAutoCheck && !AutoCheck()) return; //自动验证不通过
            if (!Check()) return; //手工验证失败

            TakeBtnClick();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ResetSize();

            _SetFontSize();
        }
        //提取明细按钮点击时候触发
        protected virtual void TakeBtnClick() { }

        private void tspClAll_Click(object sender, EventArgs e)
        {
            CheckRootNodes(true); //全选
        }

        private void CheckRootNodes(bool nodeChecked)
        {
            foreach (TreeNode node in this.treeMain.Nodes)
            {
                node.Checked = nodeChecked;
                CheckAllChildNodes(node, nodeChecked);
            }
        }

        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        private void tspUnCLAll_Click(object sender, EventArgs e)
        {
            CheckRootNodes(false); //取消全选
        }

        protected override void SaveData()
        {
            if (IsAutoCheck && !AutoCheck()) return; //自动验证不通过
            if (!Check()) return; //手工验证失败

            Hashtable data = this.tbSummary.GetAllValue();
            //除了表格控件中的值，也许客户端还需要保存其他的值到服务器端，如果Hashtable中的键和表格中的冲突了，以本次设置为准
            Hashtable otherdb = this.SaveOtherData();
            if (hasError) return;
            // 将子页面中返回的数据和表格控件中的数据合并生成jSON字符串，返回服务器处理
            string json = StringUtil.Entity.MergeToJson(true, data, otherdb);

            if (string.IsNullOrEmpty(this.SaveCallFuntion)) return;

            DataTable treedt = SetBackTreeData();//设置Tree的回写数据
            if (hasError) return;
            string errmsg = CommonHelper.Entity.SaveDtl(SaveCallFuntion, json, this.Action, treedt, null); //调用服务器的保存方法

            bool isSuccess = string.IsNullOrEmpty(errmsg); // 是否保存成功
            if (!isSuccess)
            {
                MessageBox.Show(errmsg, "提示");
                treedt = null; //清空明细数据
            }
            else
            {
                MessageBox.Show("数据保存成功！", "提示");

                if (!IsAfterSaveCloseWin) //保存后如果不关闭窗口的话，则清空界面
                {
                    this.tbSummary.Clear();
                    this.tbSummary.FocusFirst();
                }
            }
            //保存后调用
            AfterSaveData(isSuccess);

            if (IsAfterSaveCloseWin) //保存后自动关闭窗口
            {
                this.Close();
            }
        }

        //设置Tree的回写数据
        protected virtual DataTable SetBackTreeData()
        {
            return null;
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            return base.BeforePopWin(name, ref where, ref others);
        }

        public override void AfterPopWin(string name, Hashtable data, bool ischange)
        {
            base.AfterPopWin(name, data, ischange);
        }

        void _SetFontSize()
        {
            this.AutoScaleMode = AutoScaleMode.None;
            this.treeMain.Font = UI.Lib.Global.AllFont;
        }
    }
}
